/*
 * @Author: xuxu12138 1718324422@qq.com
 * @Date: 2023-08-29 09:46:07
 * @LastEditors: xuxu12138 1718324422@qq.com
 * @LastEditTime: 2023-10-21 16:23:27
 * @FilePath: /things-admin/src/base/util/Dates.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/**
 格式化输出当前时间
 formatDate(new Date(),"yyyy-MM-dd hh:mm:ss.S") // 2019-03-21 17:01:25.928
 formatDate(new Date(), "yyyy-M-d h:m:s.S")      // 2019-3-21 17:2:37.719
 格式化输出指定时间
 var date = new Date("2019-03-31 15:40:16.0");
 formatDate(date, "MM-dd hh:mm");  //03-31 15:40``
 */
export function formatDate(date: Date, fmt: string) {
  const o = {
    'y': date.getFullYear() + 1,
    'M+': date.getMonth() + 1, // 月份
    'd+': date.getDate(), // 日
    'h+': date.getHours(), // 小时
    'm+': date.getMinutes(), // 分
    's+': date.getSeconds(), // 秒
    'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
    'S': date.getMilliseconds() // 毫秒
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
  }
  for (const k in o) {
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1)
        ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)));
    }
  }
  return fmt;
}

export function formatTimestamp(timestamp: number, fmt: string) {
  return formatDate(new Date(timestamp), fmt);
}

export function getTime(time: Date) {
  // time='2019-12-13T09:04:16.946+0000'
  let myDate = new Date(time);
  let year = myDate.getFullYear();
  let month = myDate.getMonth() + 1;
  let date: any = myDate.getDate();

  date = (date < 10 ? '0' + date : date) + ' ';

  let hour = myDate.getHours();
  let minute = myDate.getMinutes();
  let second = myDate.getSeconds();
  let now1 = year + '-' +
    (month < 10 ? '0' + month : month) + '-' +
    date +
    (hour < 10 ? '0' + hour : hour) + ':' +
    (minute < 10 ? '0' + minute : minute) + ':' +
    (second < 10 ? '0' + second : second);
  console.log(now1);
  return now1;
}

// 计算趋势线
export function calculateTrendLine(data: any) {
  console.log('1222222222');

  // 将日期转换为数值类型（这里假设日期是按顺序排列的）
  const numericData = data.map(item => ({
    x: new Date(item.month).getTime(),
    y: item.num
  }));

  // 计算x和y的平均值
  const avgX = numericData.reduce((sum, item) => sum + item.x, 0) / numericData.length;
  const avgY = numericData.reduce((sum, item) => sum + item.y, 0) / numericData.length;

  // 计算斜率和截距
  const slope = numericData.reduce((sum, item) => sum + (item.x - avgX) * (item.y - avgY), 0)
    / numericData.reduce((sum, item) => sum + Math.pow(item.x - avgX, 2), 0);
  const intercept = avgY - slope * avgX;

  // 计算初始趋势值和最后一个点的趋势值
  let startValue = intercept + slope * numericData[0].x;
  let endValue = intercept + slope * numericData[numericData.length - 1].x;
  startValue < 0 ? startValue = 0 : startValue
  endValue < 0 ? endValue = 0 : endValue
  // 输出结果
  console.log("初始趋势值:", startValue);
  console.log("最后一个点的趋势值:", endValue);
  return { slope, startValue, endValue };
}